package easy;

import util.PrintUtils;

public class Solution_728 {

    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int row = image.length;
        int col = image[0].length;
        int oldVal = image[sr][sc];
        for (int i = sc; sc < col; sc++) {
            for (int j = sr - 1; j >= 0; j--) {
                if (oldVal != image[i][j]) {
                    break;
                }
                image[i][j] = newColor;
            }
            for (int j = sr; j < row; j++) {
                if (oldVal != image[i][j]) {
                    break;
                }
                image[i][j] = newColor;
            }
        }
        for (int i = sc - 1; sc >= 0; sc--) {
            for (int j = sr - 1; j >= 0; j--) {
                if (oldVal != image[i][j]) {
                    break;
                }
                image[i][j] = newColor;
            }
            for (int j = sr; j < row; j++) {
                if (oldVal != image[i][j]) {
                    break;
                }
                image[i][j] = newColor;
            }
        }
        return image;
    }

    /*public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int row = image.length;
        int col = image[0].length;
        boolean[][] flags = new boolean[row][col];
        Queue<Index> queue = new LinkedList<>();
        queue.add(new Index(sr, sc));
        flags[sr][sc] = true;
        while (!queue.isEmpty()) {
            Index index = queue.poll();
            if (index.y - 1 >= 0 && !flags[index.x][index.y-1]) {
                if (image[index.x][index.y-1] == image[index.x][index.y]) {
                    queue.add(new Index(index.x, index.y-1));
                }
                flags[index.x][index.y-1] = true;
            }
            if (index.y + 1 < col && !flags[index.x][index.y+1]) {
                if (image[index.x][index.y+1] == image[index.x][index.y]) {
                    queue.add(new Index(index.x, index.y+1));
                }
                flags[index.x][index.y+1] = true;
            }
            if (index.x - 1 >= 0 && !flags[index.x-1][index.y]) {
                if (image[index.x-1][index.y] == image[index.x][index.y]) {
                    queue.add(new Index(index.x-1, index.y));
                }
                flags[index.x-1][index.y] = true;
            }
            if (index.x + 1 < row && !flags[index.x+1][index.y]) {
                if (image[index.x+1][index.y] == image[index.x][index.y]) {
                    queue.add(new Index(index.x+1, index.y));
                }
                flags[index.x+1][index.y] = true;
            }
            image[index.x][index.y] = newColor;
        }
        return image;
    }

    class Index {
        public int x;
        public int y;

        public Index(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }*/

    public static void main(String[] args) {
        Solution_728 model = new Solution_728();
        int[][] ints = model.floodFill(new int[][]{{1, 1, 1}, {1, 1, 0}, {1, 0, 1}}, 1, 1, 2);
        for (int[] anInt : ints) {
            PrintUtils.arrayInt(anInt);
        }
    }
}
